package com.xixi.controller;
import com.xixi.model.RegionState;
import com.xixi.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
public class DataCroller {
    @Autowired
    private MyService myService;

    @RequestMapping("/data/china")
    public Map<String, Object> returnChina() throws IOException {
        List<RegionState> regionStates = myService.getRegionStates("China");
        Map<String,Object> tableData =new HashMap<String,Object>();

        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");

        tableData.put("data", regionStates);
        return tableData;
    }

    @RequestMapping("/data/all")
    public Map<String, Object> returnAll() throws IOException {
        List<RegionState> num = myService.getNum();
        Map<String,Object> tableData =new HashMap<String,Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");

        tableData.put("data", num);
        return tableData;
    }

    @RequestMapping("/data/country")
    public Map<String, Object> findCountry(String country) throws IOException {
        //System.out.println(country+"=============================");
        Map<String,Object> tableData =new HashMap<String,Object>();
        tableData.put("code", 0);
        tableData.put("msg", "");
        List<RegionState> num = myService.getNum();
        for(RegionState res : num)
        {
            if(res.getCountry().equals(country))
            {
                LinkedList<RegionState> regionStates = new LinkedList<>();
                regionStates.add(res);
                tableData.put("data",regionStates);
            }
        }
        tableData.putIfAbsent("data", null);
        //System.out.println(data.toString());
        return tableData;
    }

    @RequestMapping("/data/city")
    public Map<String, Object> findCity(String state) throws IOException {
        Map<String,Object> tableData =new HashMap<String,Object>();
        List<RegionState> regionStates = myService.getRegionStates("China");
        tableData.put("code", 0);
        tableData.put("msg", "");
        if(state!=null)
            System.out.println(state);
        for (RegionState regionState : regionStates) {
            if (regionState.getState().equals(state))
            {
                List<RegionState> list = new LinkedList<>();
                list.add(regionState);
                tableData.put("data", list);
            }
        }
        tableData.putIfAbsent("data", null);
        return tableData;
    }

    @RequestMapping("/data/color")
    public Map<String,Object> getColorNum() throws IOException {
        Map<String,Object> tableData =new HashMap<String,Object>();
        tableData.put("msg", "success");
        List<RegionState> num = myService.getNum();
        String[] name = new String[num.size()];
        int[] sum = new int[num.size()];
        int i = 0;
        for(RegionState regionState : num)
        {
            name[i] = regionState.getCountry();
            sum[i] = regionState.getLatestTotalCase();
            i++;
        }
        tableData.put("country", name);
        tableData.put("num", sum);

        return tableData;
    }

    @RequestMapping("/color/china")
    public Map<String, Object> getChinaColor() throws IOException {
        Map<String,Object> tableData =new HashMap<String,Object>();
        List<RegionState> regionStates = myService.getRegionStates("China");
        String[] names = new String[regionStates.size()];
        int[] nums = new int[regionStates.size()];
        int i = 0;
        tableData.put("msg", "success");
        for(RegionState regionState : regionStates)
        {
            names[i] = regionState.getState();
            nums[i] = regionState.getLatestTotalCase();
            i++;
        }
        tableData.put("name", names);
        tableData.put("value", nums);

        return tableData;
    }
}
